import requests
import re
from fontTools.ttLib import TTFont
from lxml import etree
import base64
import time
from urllib.parse import unquote,quote

result = []

for i in range(1,1001):
    url = 'http://glidedsky.com/level/web/crawler-font-puzzle-2?page={}'.format(i)
    print(url)
    headers = {
        'Cookie':'_ga=GA1.2.682296989.1618206060; _gid=GA1.2.791098064.1618206060; footprints=eyJpdiI6IlNTdWI0WGFTZzIxeGtpVVdwbTVXbWc9PSIsInZhbHVlIjoiOGRIa0NlK3laN1lwRmZKcHp0cjFGR2FpNzdHR05DaTlNXC9oK2JmUE5ONHR3RkxUendsNmhmQnBldXRubHlFVEQiLCJtYWMiOiIxNDFlNTM4NTRjZTFiYWRkOGY2MDE3OGEwNzNlZjdjNDNjMGNhN2M5MTcwODk5ZTk0YmNlOTNhMTFhN2E3NTM4In0%3D; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6InNHYjAyNHdYSTdjcXRzaEpRekNaVkE9PSIsInZhbHVlIjoibytFNldBZUdOVXFlTW5aMU1LV2FFNUw3NEpWTW83eDBRM0VOeXoxNmJlYzVnc0I3VThTcWs2RHB0OHE5QXFSZEtaWWZOOTVXdTJVekN1enFxdzlYUWZTUTM3cFZmcnI2blQ4S0w1djN2bkdQVVczN2x0TXFXbXhTVFwvUlwvOFwvMnZ5M0VhdzVIZGRLbENINWJqVDdEMFFPTDlTaUxTb25DUnJvY29hdWw5bFpZPSIsIm1hYyI6ImEwZGUxZjkxYWE5MGRiOTYzNWQ4OTgxNGJhNzZkNDk2N2IxNGE3NjFiYTE5MGY3NWU3MTM5OGFmYTVhM2IyZDEifQ%3D%3D; Hm_lvt_020fbaad6104bcddd1db12d6b78812f6=1618206060,1618366606; XSRF-TOKEN=eyJpdiI6ImdHeDJUN2RidjI1ODVwRk13dXM4SVE9PSIsInZhbHVlIjoiaXlUdmlqcVRmTDRVYlZhdUtJK2ZlSTJpWEtDXC93T2RaWENcL2JQXC85MjhYYyt1UkwxZlllYWxZdmJIUTJlMGRrSCIsIm1hYyI6IjFlMzQ1YWQyNTU2Y2Y1NzcxNWMxNjA0Y2IwMjZkYzNkY2M1NWU2NWIwOWIwMjc4NjgyZDhmZDEzNzUzZGE2YzMifQ%3D%3D; glidedsky_session=eyJpdiI6ImJcLzdTVWRCbStHZ2pSUW92TGppODR3PT0iLCJ2YWx1ZSI6IndwT0xaOWhZcEV0eFRKdGM0NEFMSXRhODFUczhDZ1VkaVQwV1hvMU5qTGVURW9OckprNEp0XC9NSVR5c2xYVGZDIiwibWFjIjoiZmFmN2I3MjJlMDc1OTdlYjNmOTI5YjBlYzUyYzgzOWI0NGI1MjkyZWRlOTEzOThmMzQzNzNkNmQ4OTE2MGQwOCJ9; Hm_lpvt_020fbaad6104bcddd1db12d6b78812f6=1618369818',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text

    with open('字体2.html','w',encoding='utf-8') as f:
        f.write(response)

    font_base = re.findall(';base64,(.*?)\) format',response)[0]

    with open('font2.ttf','wb') as f:
        f.write(base64.b64decode(font_base))

    font = TTFont('font2.ttf')
    font.saveXML('font2.xml')

    font = TTFont('font2.ttf')
    font.saveXML('font2.xml')

    font_map = font.getGlyphOrder()
    font_cmap = font['cmap'].getBestCmap()
    font_cmap = dict(zip(font_cmap.values(), font_cmap.keys()))
    # print(font_map)
    # print(font_cmap)

    lists = []
    font_map_new = []
    for name in font_map[1:11]:
        # print(font.getGlyphID(name))
        if name == '.notdef':
            continue
        lists.append(font.getGlyphID(name) -1)
        font_map_new.append(name)

    dicts = dict(zip(font_map_new,lists))
    # print(lists)
    # print(font_map_new)

    t1 = []
    t2 = []
    for key in dicts.keys():
        t1.append(chr(font_cmap[key]))
        t2.append(dicts[key])

    t3 = dict(zip(t1,t2))

    font_xpath = "//div[@class='col-md-1']/text()"
    html = etree.HTML(response)
    data = html.xpath(font_xpath)
    for i in data:
        num = ''
        for j in i.strip():
            num += str(t3[j])
        result.append(int(num))
    # time.sleep(1)
    print(sum(result))
print(sum(result))

